package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.AnswerListAdapter
import com.lancoo.znbkxx.teacher.adapter.StuImageAdapter
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtClassicalBinding
import com.lancoo.znbkxx.teacher.model.data.Classical
import com.lancoo.znbkxx.teacher.utils.GlideEngine
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.entity.LocalMedia

import kotlinx.android.synthetic.main.fmt_classical.*

/**
 * Created by wealive on 2021/4/14.
 *
 *
 */
class ClassicalFragment(private val onclickListener: OnClickClassicalListener,
        private val onAudioClickListener: AnswerListAdapter.OnAudioClickListener) :
        BaseVMFragment<HardSolutionViewModel, FmtClassicalBinding>(),
        BaseQuickAdapter.OnItemClickListener {
    private val item by lazy { requireArguments().getParcelable<Classical>("item") }
    private val dialogTag by lazy { requireArguments().getString("dialogTag") }
    private val position by lazy { requireArguments().getInt("position") }
    override fun providerContext(): Any? {
        return this
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.fmt_classical
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        tv_stuName.text = "${item!!.StuName}的作答"
        btn.setOnClickListener {
            onclickListener.onClickClassical(btn, position)
        }
        if (dialogTag == "add") {
            btn.visibility = View.VISIBLE
            if (item!!.IsSelected == 0) {
                btn.apply {
                    tag = "unselected"
                    text = "选用"
                    setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,
                            R.mipmap.aibk_icon_unselect_classical), null, null, null)
                }
            } else {
                btn.apply {
                    tag = "selected"
                    text = "取消选用"
                    setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,
                            R.mipmap.aibk_icon_select_classical), null, null, null)
                }
            }
        } else {
            btn.visibility = View.GONE
        }
        val adapter = AnswerListAdapter(
                R.layout.item_stu_answer,
                item!!.AnswerList,
                onAudioClickListener,
                position
        )
        adapter.onItemClickListener = this
        rv_stuAnswer.layoutManager = LinearLayoutManager(context)
        rv_stuAnswer.adapter = adapter
    }

    interface OnClickClassicalListener {
        fun onClickClassical(textView: TextView, position: Int)
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        if (adapter is StuImageAdapter) {
            PictureSelector.create(this).themeStyle(R.style.picture_default_style)
                    .imageEngine(GlideEngine.createGlideEngine())
                    .openExternalPreview(position, adapter.data)
        }
    }
}